//
//  TimeSetView.m
//  project
//
//  Created by 圆圆陈 on 2017/12/23.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "TimeSetView.h"

@interface TimeSetView ()
@property(nonatomic, strong) NSString *returnStr;
@end

@implementation TimeSetView {
    UIView       *backView;
    UIDatePicker *startPicker;
    UIDatePicker *endPicker;
}

- (instancetype)initWithFrame:(CGRect)frame withType:(TimeSetType)type {
    if (self = [super initWithFrame:frame]) {
        _returnStr               = [NSString string];
        _type                    = type;
        backView                 = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT*2/3, HHHWIDTH, HHHHEIGHT/3)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(70), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        CGSize titleSize = [cancelBtn.titleLabel.text sizeWithFont:cancelBtn.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, FitH(30))];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), titleSize.width, FitH(30));
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        sureBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50), HHHWIDTH, 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 这里是用大写的 H
        NSString *dateStr = [formatter stringFromDate:[NSDate date]];
        NSDate   *date    = [formatter dateFromString:dateStr];
        CGFloat widthPick = 0;
        if (IOS88884444) {
            widthPick = - HHHWIDTH/4;
        };
        startPicker                 = [[UIDatePicker alloc] initWithFrame:CGRectMake(widthPick, line.bottom+FitH(20), HHHWIDTH/2, backView.height-FitH(90))];
        startPicker.datePickerMode  = UIDatePickerModeTime;
        startPicker.backgroundColor = [UIColor clearColor];
        startPicker.tag             = 100;
        startPicker.date            = date;
        //startPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
        startPicker.locale = [NSLocale currentLocale];
        [startPicker addTarget:self
                        action:@selector(pickerChanged:)
              forControlEvents:UIControlEventValueChanged];
        [startPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
        [backView addSubview:startPicker];
        
        NSArray *arr = @[@"", @"--", @""];
        for (int i = 0; i < arr.count; i++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FitW(20), FitW(20))];
            label.center    = CGPointMake(HHHWIDTH/4*(i+1), startPicker.centerY);
            label.text      = arr[i];
            label.textColor = SNCOLORBLACK;
            [backView addSubview:label];
        }
        
        endPicker                 = [[UIDatePicker alloc] initWithFrame:CGRectMake(widthPick+HHHWIDTH/2, line.bottom+FitH(20), HHHWIDTH/2, backView.height-FitH(90))];
        endPicker.datePickerMode  = UIDatePickerModeTime;
        endPicker.backgroundColor = [UIColor clearColor];
        endPicker.tag             = 101;
        endPicker.date            = date;
        //endPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
        endPicker.locale = [NSLocale currentLocale];
        [endPicker addTarget:self
                      action:@selector(pickerChanged:)
            forControlEvents:UIControlEventValueChanged];
        [endPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
        [backView addSubview:endPicker];
        NSString *tmpStr = [dateStr substringWithRange:NSMakeRange(0, 11)];
        if (_type == HeartAuto) {
        } else if (_type == TwistLight) {
        } else {
            NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.startH, SN_USER.startM];
            startPicker.date = [NSDate dateWithDate:[formatter dateFromString:startStr]];
            NSString *endStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00", tmpStr, SN_USER.endH, SN_USER.endM];
            endPicker.date = [NSDate dateWithDate:[formatter dateFromString:endStr]];
        }
    }
    return self;
}

- (void)cancelBtnClick {
    [self animationedDown];
}

- (void)sureBtnClick {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"HH:mm"];
    NSString *start = [formatter stringFromDate:startPicker.date];
    NSString *end   = [formatter stringFromDate:endPicker.date];
    _returnStr = [NSString stringWithFormat:@"(%@-%@)", start, end];
    if (_type == HeartAuto) {
    } else if (_type == TwistLight) {
    } else {
        SN_USER.startH = startPicker.date.hour;
        SN_USER.startM = startPicker.date.minute;
        SN_USER.endH   = endPicker.date.hour;
        SN_USER.endM   = endPicker.date.minute;
        [KK_BLESend sendNotDisturbWithBlock:^(id object, KKBLEAcceptType type) {
        }];
    }
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr);
    }
    [SN_USER updateToDB];
    [self animationedDown];
}

- (void)pickerChanged:(UIDatePicker *)picker {
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}

@end

